﻿/******************************************************************************
* File description: SpeechMod.qml
* Function: 语音模块页面
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import QtMultimedia 5.12
import "../components"
Rectangle {
    width: parent.width
    height: parent.height
    radius: 12
    color: "white"

    property int pageType: 1

    Connections{

    }
    onVisibleChanged: {
        downloadIpInfo()
        refreshDefaultCameraList()
    }

    onPageTypeChanged: {
        downloadIpInfo()
    }


    property var defaultCameraList: [];

    function refreshDefaultCameraList(){
        var cameraList = QtMultimedia.availableCameras
        defaultCameraList = []
        for(let i = 0; i < cameraList.length; i ++){
            defaultCameraList.push(cameraList[i].displayName + "(" + cameraList[i].deviceId[24] + cameraList[i].deviceId[25] + cameraList[i].deviceId[26] + cameraList[i].deviceId[27] + ")")
        }
        defaultCameraListChanged()
        console.log("刷新默认相机列表", defaultCameraList)
    }


    function downloadIpInfo(){
        var infoList = CCameraInterface.getCameraInfo(pageType)

        combo_cameraType.setText(infoList[0])
        edit_ip      .setText(infoList[1])
        edit_port    .setText(infoList[2])
        edit_username.setText(infoList[3])
        edit_password.setText(infoList[4])


    }
    function uploadIpInfo(){

        if(combo_cameraType.getText() === "HIK"){
            CCameraInterface.connectCamera(pageType, "HIK", edit_ip.getText(), edit_port.getText(), edit_username.getText(), edit_password.getText())
        }else{
            CCameraInterface.connectCamera(pageType, "JIF", edit_ip.getText(), edit_port.getText(), edit_username.getText(), edit_password.getText())
        }

    }

    Component.onCompleted: {
        var buttonList = row_button.children          // 初始化按钮样式，TCP删除了所以不用了
        for(let i = 0; i < buttonList.length; i ++){
            buttonList[i].button_color = "transparent"
            buttonList[i].button_color_select = "white"
            buttonList[i].radius = 4*dp
        }
    }

    // TAB按钮 ================================================
    Rectangle{
        color: "#EBEEF3"
        anchors.horizontalCenter: parent.horizontalCenter
        y: 60*dp
        height: 54*dp; width: row_button.width + 4*dp
        radius: 4*dp
        Row{
            id: row_button
            x: 2*dp; y: 2*dp
            CustomButton{
                title: "放靶/转靶"
                width: 110*dp; height: 50*dp
                isSelected: pageType === 1
                onClicked: {
                    pageType = 1
                }
            }
            CustomButton{
                title: "打靶"
                width: 110*dp; height: 50*dp
                isSelected: pageType === 2
                onClicked: {
                    pageType = 2
                }
            }
            CustomButton{
                title: "纯化"
                width: 110*dp; height: 50*dp
                isSelected: pageType === 4
                onClicked: {
                    pageType = 4
                }
            }

        }

    }

    Column{
        y: 140*dp
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 16*dp
        CustomComboBox{
            id: combo_cameraType
            title: "摄像头类型"
            model: ["HIK", "JIF"]
            required: true
            editWidth: 350*dp
            titleWidth: 100*dp
        }

        CustomComboBox{
            id: combo_deviceName
            title: "摄像头名称"
            editWidth: 350*dp
            titleWidth: 100*dp
            model: defaultCameraList
            visible: false      // usb摄像头暂时不启用
        }

        CustomInputText{
            id: edit_ip
            title: "IP地址"
            required: true
            editWidth: 350*dp
            titleWidth: 100*dp
            Keys.onTabPressed:{
                edit_port.forceActiveFocus()
            }
        }
        CustomInputText{
            id: edit_port
            title: "端口号"
            required: true
            editWidth: 350*dp
            titleWidth: 100*dp
            Keys.onTabPressed:{
                edit_username.forceActiveFocus()
            }
        }
        CustomInputText{
            id: edit_username
            title: "用户名"
            editWidth: 350*dp
            titleWidth: 100*dp
            Keys.onTabPressed:{
                edit_password.forceActiveFocus()
            }
        }
        CustomInputText{
            id: edit_password
            title: "密码"
            editWidth: 350*dp
            titleWidth: 100*dp
            Keys.onTabPressed:{
                edit_ip.forceActiveFocus()
            }
        }
    }

    CustomButton{
        x: parent.width*0.5 + 100*dp;  y: 480*dp
        title: "连接"
        defaultMode1: true
        onClicked: {
            if(edit_ip.getText()==="" || edit_port.getText()===""){
                toast_settings.show("请检查输入是否完整", 1)
            }

            uploadIpInfo()
        }
    }

    Connections{
        target: CSignal
        onCameraConnect: {
            toast_settings.show(connState?"连接成功":"连接失败", connState?0:1)
        }
    }
}
